<template>
  <print-designer :temp-value="value" :widget-options="widgets" @save="handleSave"></print-designer>
</template>

<script>
import PrintDesigner from '@/components/PrintDesigner/index'
import { getTemp, updateTemp } from '@/api/tool/labelTemp'

export default {
  name: 'TempDesigner',
  components: {
    PrintDesigner
  },
  data() {
    return {
      labelTempId: '',
      value: {
        title: 'demo',
        width: 750,
        height: 550,
        pageWidth: 750,
        pageHeight: 550,
        tempItems: []
      },
      widgets: [
        {
          type: 'braid-txt',
          isEdit: 1,
          title: '样本复份编号',
          value: '{样本复份编号}',
          defaultValue: '',
          name: 'sampBackupCode'
        },
        {
          type: 'bar-code',
          isEdit: 1,
          title: '条码/二维码',
          value: '{条码/二维码}',
          defaultValue: '',
          name: 'qrCode'
        },
        {
          type: 'braid-txt',
          isEdit: 1,
          title: '样本量',
          value: '{样本量}',
          defaultValue: '',
          name: 'sampQuantity'
        },
        {
          type: 'braid-txt',
          isEdit: 1,
          title: '样本单位',
          value: '{样本单位}',
          defaultValue: '',
          name: 'sampUnit'
        },
        {
          type: 'braid-txt',
          isEdit: 1,
          title: '采样时间',
          value: '{采样时间}',
          defaultValue: '',
          name: 'samplingDate'
        },
        {
          type: 'braid-txt',
          isEdit: 1,
          title: '样本类型名称',
          value: '{样本类型名称}',
          defaultValue: '',
          name: 'sampTypeName'
        },
        {
          type: 'braid-txt',
          isEdit: 1,
          title: '样本源名称',
          value: '{样本源名称}',
          defaultValue: '',
          name: 'sampSourceName'
        },
        {
          type: 'braid-txt',
          isEdit: 1,
          title: '样本源类型名称',
          value: '{样本源类型名称}',
          defaultValue: '',
          name: 'sampSourceTypeName'
        },
        {
          type: 'braid-txt',
          isEdit: 1,
          title: '样本组名称',
          value: '{样本组名称}',
          defaultValue: '',
          name: 'sampGroupName'
        },
        {
          type: 'braid-txt',
          isEdit: 1,
          title: '样本源编号',
          value: '{样本源编号}',
          defaultValue: '',
          name: 'sampSourceCode'
        },
        {
          type: 'braid-txt',
          isEdit: 1,
          title: '样本源描述',
          value: '{样本源描述}',
          defaultValue: '',
          name: 'sampSourceDesc'
        }
      ]
    }
  },
  created() {
    this.labelTempId = this.$route.params && this.$route.params.labelTempId
    this.getLabelTemp(this.labelTempId)
  },
  methods: {
    getLabelTemp(labelTempId) {
      getTemp(labelTempId).then(response => {
        let labelTemp = response.data
        if (labelTemp) {
          this.value.title = labelTemp.labelTempName
          this.value.width = labelTemp.labelWidth
          this.value.height = labelTemp.labelHeight
          this.value.pageWidth = labelTemp.labelPageWidth
          this.value.pageHeight = labelTemp.labelPageHeight
        }
        if (labelTemp.labelTempContent) {
          this.value.tempItems = JSON.parse(labelTemp.labelTempContent).tempItems
        }
      })
    },
    handleSave(data) {
      let labelTemp = {}
      labelTemp.labelTempId = this.labelTempId
      labelTemp.labelTempName = this.value.title
      labelTemp.labelTempContent = JSON.stringify(data)
      updateTemp(labelTemp).then(response => {
        this.msgSuccess('保存成功')
        this.$router.back()
      })
    }
  }
}
</script>
